package com.linnas.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linnas.common.R;
import com.linnas.dto.PetDto;
import com.linnas.pojo.Category;
import com.linnas.pojo.Pet;
import com.linnas.service.CategoryService;
import com.linnas.service.PetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class PetController {

    @Autowired
    private PetService petService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 信息分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<PetDto>> page(int page, int pageSize, String name) {
        Page<Pet> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Pet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), Pet::getName, name);
        queryWrapper.orderByDesc(Pet::getUpdateTime);
        petService.page(pageInfo, queryWrapper);

        Page<PetDto> petDtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, petDtoPage, "records");
        List<Pet> records = pageInfo.getRecords();
        List<PetDto> petDtoList = records.stream().map((item) -> {
            PetDto petDto = new PetDto();
            BeanUtils.copyProperties(item, petDto);
            Long categoryId = item.getCategoryId();
            String categoryName = categoryService.getById(categoryId).getName();
            if (StringUtils.isNotBlank(categoryName)) {
                petDto.setCategoryName(categoryName);
            }
            return petDto;
        }).collect(Collectors.toList());

        petDtoPage.setRecords(petDtoList);
        return R.success(petDtoPage);
    }

    /**
     * 新增宠物
     *
     * @param pet
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Pet pet) {
        log.info("新增宠物信息: {}", pet);
        petService.save(pet);
        return R.success("添加宠物成功");
    }

    /**
     * 修改宠物信息
     *
     * @param petDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody PetDto petDto) {
        log.info("要修改的宠物信息: {}", petDto);
        Pet pet = new Pet();
        BeanUtils.copyProperties(petDto, pet, "categoryName");
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(Category::getName, petDto.getCategoryName());
        Category category = categoryService.getOne(categoryLambdaQueryWrapper);
        pet.setCategoryId(category.getId());
        petService.updateById(pet);
        return R.success("修改宠物信息成功");
    }

    /**
     * 根据id查询宠物信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<PetDto> getById(@PathVariable Long id) {
        PetDto petDto = new PetDto();
        Pet pet = petService.getById(id);
        String categoryName = categoryService.getById(pet.getCategoryId()).getName();
        petDto.setCategoryName(categoryName);
        BeanUtils.copyProperties(pet, petDto);
        return R.success(petDto);
    }

    /**
     * 修改商品销售状态 0: 禁售 1: 起售
     *
     * @param ids
     * @param state
     * @return
     */
    @PostMapping("/status/{state}")
    public R<String> updateStatus(@RequestParam List<Long> ids, @PathVariable Integer state) {
        log.info("请求id为: {}", ids.toString());
        log.info("当前状态为:{}", state);
        List<Pet> pets = petService.listByIds(ids);

        pets.forEach((item) -> {
            item.setStatus(state);
            petService.updateById(item);
            log.info("item: {}", item.toString());
        });

        log.info(pets.toString());
        return R.success("修改销售状态成功");
    }

    /**
     * 删除宠物
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        petService.removeBatchByIds(ids);
        return R.success("删除宠物成功");
    }
}
